//
//  ESNetTask.m
//  EasyStyle
//
//  Created by FK on 15/12/24.
//  Copyright © 2015年 FK. All rights reserved.
//

#import "ESNetTask.h"
#import "ESNetRequestWork.h"

@implementation ESNetTask

- (void)startNetRequestTask:(NSString *)requestType forParam:(NSDictionary *)paramDict andResult:(ESNetResult *)netResult
{
    [ESNetRequestWork NetRequestGETWithRequestURL:requestType
                                    WithParameter:paramDict
                             WithReturnValeuBlock:^(id returnValue) {
                                 [self fetchDataSuccessWithResultData:returnValue andResult:netResult];
                             } WithFailureBlock:^{
                                 [self netFailure];
                             }];
    
    
}

#pragma 获取到正确的数据，对正确的数据进行处理
- (void)fetchDataSuccessWithResultData:(NSData *)resultData andResult:(ESNetResult *)netResult
{
    if ([netResult respondsToSelector:@selector(parseNetResult:)]) {
        
#if DEBUG
        NSString *result  =[[ NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", result);
#endif
        NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingAllowFragments error:nil];
        [netResult parseNetResult:jsonDictionary];
        
        if ([netResult.errorCode intValue] != 0) {
            [self errorCodeWithDic:nil];
        } else {
            self.returnBlock(netResult);
        }
    }
}

#pragma 对ErrorCode进行处理
-(void)errorCodeWithDic: (NSDictionary *) errorDic
{
    self.errorBlock(errorDic);
}

#pragma 对网路异常进行处理
-(void)netFailure
{
    self.failureBlock();
}


@end
